package com.soup.lab.basic.exec;

import java.util.concurrent.BlockingQueue;

public class BizThread extends Thread {

    private RuntimeContext context;

    private BlockingQueue<Runnable> queue;

    private String bizKey;

    @Override
    public void run() {
        Runnable task;
        while ((task = takeTask()) != null) {
            task.run();
        }
    }

    private Runnable takeTask() {
        Runnable r = queue.poll();
        if(r != null) {
            return r;
        }
        if(context.fetch()) {
            return queue.poll();
        }
        return null;
    }


    public RuntimeContext getContext() {
        return context;
    }

    public void setContext(RuntimeContext context) {
        this.context = context;
    }

    public BlockingQueue getQueue() {
        return queue;
    }

    public void setQueue(BlockingQueue queue) {
        this.queue = queue;
    }

    public String getBizKey() {
        return bizKey;
    }

    public void setBizKey(String bizKey) {
        this.bizKey = bizKey;
    }
}
